package com.yc.navy.blog.blog.controller;

import com.yc.navy.blog.blog.bean.ArticleInfo;
import com.yc.navy.blog.blog.bean.UserInfo;
import com.yc.navy.blog.blog.service.IArticleInfoService;
import com.yc.navy.blog.blog.util.ConstantInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Company 海洋信息工程
 *
 * @author navy
 * @date 2024/9/6
 * Email haijunzhou@ynit.edu.cn
 */

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private  IArticleInfoService articleService;




    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public Map<String, Object> upload(MultipartFile upload) {
        Map<String,Object> map=new HashMap<>();
        if (upload !=null || upload.getSize() >0) {

            try{
                String savepath = ConstantInfo.uploadPath +"/"+ System.currentTimeMillis() + "_" + upload.getOriginalFilename();
                File dest = new File(ConstantInfo.basepath,savepath);
                upload.transferTo(dest);
                map.put("filename",upload.getOriginalFilename());
                map.put("uploaded", 1);
                map.put("url", "../../" + savepath);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }


        return map;
    }

    /**
     * 添加文章
     */
    @PostMapping("/add")
    public ResultVO add(ArticleInfo af, MultipartFile photo, HttpSession session) {
        Object obj=session.getAttribute(ConstantInfo.CURRENTLOGINUSER);

        if (obj == null) {
            return new ResultVO(ResultEnum.LOGIN_TIMEOUT);
        }
        UserInfo uf=(UserInfo) obj;
        af.setUsid(uf.getUsid());
        if (photo!= null && photo.getSize()>0) {
            try {
                String savepath = ConstantInfo.uploadPath +"/"+ System.currentTimeMillis() + "_" + photo.getOriginalFilename();
                File filepath = new File(ConstantInfo.basepath,savepath);
                pthoo.transferTo(dest);
                af.setPic(savepath);

            } catch (IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        int result = articleService.add(af);
        if (result == -100) {
            return new ResultVO(ResultEnum.CHEKK_ERROR);
        }

        if (result > 0) {
            return new ResultVO(ResultEnum.SUCCESS);
        }

        return new ResultVO(ResultEnum.ERROR);
    }

    /**
     * 文章列表
     */
    @GetMapping("/list")
    public ResultVO list(Integer page, Integer rows) {
        List<ArticleInfoDTO> list = articleInfoService.list(page, rows);
        if (list == null || list.isEmpty()) {
            return new ResultVO(ResultEnum.DATA_NULL);
        }

        return new ResultVO(ResultEnum.SUCCESS, list);
    }

    /**
     * 根据id查找文章
     */
    @GetMapping("/find")
    public ResultVO find(Long ano) {
        ArticleInfoDTO dto = articleService.find(ano);
        if (dto == null) {
            return new ResultVO(ResultEnum.DATA_NULL);
        }

        return new ResultVO(ResultEnum.SUCCESS, dto);
    }
}
